<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="item.art_id + index"
        :title="item.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from "@/api/login";
export default {
  name: "SearchResult",
  components: {},
  props: ["input"],
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 20,
      error: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    console.log("我被创建了");
  },
  mounted() {},
  methods: {
    async onLoad() {
      try {
        let re = await getSearchResult({
          page: this.page,
          per_page: this.per_page,
          q: this.input,
        });

        console.log(re);
        let {
          data: {
            data: { results },
          },
        } = re;
        this.list.push(...results);
        this.loading = false;
        // throw new Error(1);
        // 结束有2种做法
        this.page += 1;
        if (results.length <= 0) {
          this.finished = true;
        }
      } catch (error) {
        console.log(error);
        this.error = true;
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped lang="less"></style>
